#include "sprite.h"
#include "func.h"

extern PFNGLACTIVETEXTUREPROC glActiveTexture;
extern PFNGLVERTEXATTRIB2FPROC glVertexAttrib2f;

extern GLint pos,color,texcoord;

namespace sprite{

image::image(){

	this->SetSize(1);
	this->SetSquare(0,0);
	this->framenum=1;
	this->frameID=0;
	this->texname=NULL;
	this->effect=0;
	this->myindex=NULL;
	this->texwidth=0;
	this->texheight=0;

}

image::~image(){

	if(this->myindex!=NULL){

		free(this->myindex);

	}

}

void image::EnableEffect(UINT ceffect){

	this->effect = ( this->effect )|ceffect;

}

void image::DisableEffect(UINT ceffect){

	this->effect = (this->effect)&!(( this->effect )&ceffect);

}

void image::SetSize(const float magnitude){

	this->size = magnitude;

}

void image::SetFramenum(const unsigned framenum){

	this->framenum = framenum;

}

void image::GetSquare(float& width, float& height) const {

	width=this->width;
	height=this->height;

}

void image::SetSquare(const float width, const float height){

	this->width=width;
	this->height=height;

}

void image::SetFrameID(const unsigned frameID){

	this->frameID=frameID;
		
}

void image::BindTexture(const texture& tex){

	this->texname = tex.GetTex();
	tex.GetSize(this->texwidth,this->texheight);

}

void image::BindIndex(const index& index, const unsigned start, const unsigned length){

	this->myindex=(indexset*)realloc(this->myindex,sizeof(indexset)*(length-start));
	unsigned max;
	index.GetMaxIndex(max);
	indexset* k=(indexset*)malloc(sizeof(indexset)*max);
	index.GetCurrentIndex(k,max);
	memcpy(this->myindex,k,sizeof(indexset)*(length-start));
	this->SetFramenum(length-start);
	this->SetFrameID(0);
	free(k);

}

void image::Draw(HWND hWnd){

	if(this->texname){

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D,this->texname);

		RECT rt;
		GetWindowRect(hWnd,&rt);

		float s,t,u,v;

		float x,y;

		this->Getxy(x,y);

		float width, height;

		this->GetSquare(width,height);

		SamplingPos(x,y,width,height,(float)rt.right,(float)rt.bottom,s,t,u,v);

		float angle;
		this->GetAngle(angle);
		SetRotationMatrix(angle);

		float a,b,c,d;

		if(this->myindex){

			SamplingTexcoord(this->myindex[this->frameID].currentx,this->myindex[this->frameID].currenty,this->myindex[this->frameID].currentwidth,this->myindex[this->frameID].currentheight,this->texwidth,this->texheight,a,b,c,d);
					
		}

		else{

			SamplingTexcoord(0,0,1,1,1,1,a,b,c,d);
		
		}


		//GLuint bottom,left,width,height;

		/*bottom=x+(GLuint)((1-(this->size))*((this->width)/2));
		left=y+(GLuint)((1-(this->size))*((this->height)/2));
		width=(GLuint)((this->size)*(this->width));
		height=(GLuint)((this->size)*(this->height));*/

		//glViewport(bottom,left,width,height);

		glBegin(GL_QUADS);

			glVertexAttrib2f(texcoord,a,b);
			glVertexAttrib2f(pos,(s)*this->size,(t)*this->size);
			glVertexAttrib2f(texcoord,c,b);
			glVertexAttrib2f(pos,u*this->size,t*this->size);
			glVertexAttrib2f(texcoord,c,d);
			glVertexAttrib2f(pos,u*this->size,v*this->size);
			glVertexAttrib2f(texcoord,a,d);
			glVertexAttrib2f(pos,s*this->size,v*this->size);
		
			/*glColor3f(1.0,0.0,0.0);
			glVertex2f(0.0,0.0);
			glVertex2f(0.5,0.0);
			glVertex2f(0.5,0.5);
			glVertex2f(0.0,0.5);*/

		

			/*glTexCoord2f(0.0,0.0);
			glVertex2fv(mam[0]);
			glTexCoord2f(1.0,0.0);
			glVertex2fv(mam[1]);
			glTexCoord2f(1.0,1.0);
			glVertex2fv(mam[2]);
			glTexCoord2f(0.0,1.0);
			glVertex2fv(mam[3]);*/

		glEnd();

	}

}

}